package own.stu.jobgib.playown.vediodown.common.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.asynchttpclient.AsyncHttpClient;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;
import own.stu.jobgib.playown.util.IntTypeAdapter;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.asynchttpclient.Dsl.asyncHttpClient;

public class DefaultConfig {

    protected static Logger logger = LoggerFactory.getLogger(DefaultConfig.class);

    private Gson gson = new GsonBuilder()
            .registerTypeAdapter(int.class, new IntTypeAdapter())
            .registerTypeAdapter(Integer.class, new IntTypeAdapter()).create();

    private AsyncHttpClient client = asyncHttpClient();
    private RedissonClient redisson;
    private ExecutorService threadPool = Executors.newFixedThreadPool(2);
    private RestTemplate restTemplate = new RestTemplate();

    public DefaultConfig() {
        Config config = new Config();
        config.useSingleServer()
                .setConnectionPoolSize(4)
                .setConnectionMinimumIdleSize(2)
                .setSubscriptionConnectionPoolSize(4)
                .setSubscriptionConnectionMinimumIdleSize(2)
                .setAddress("redis://localhost:6379"); // 修改hosts文件

        //使用json序列化方式
        Codec codec = new JsonJacksonCodec();
        config.setCodec(codec);

        redisson = Redisson.create(config);

        init();
    }

    public void destory() throws IOException {
        client.close();
        redisson.shutdown();
        threadPool.shutdown();
    }

    private String DEFAULT_PLATFORM;
    private String SHORT_LINK_URL;

    protected void init() {
        DEFAULT_PLATFORM = "bili";
        SHORT_LINK_URL = "https://b23.tv/";
    }

    public Gson getGson() {
        return gson;
    }

    public AsyncHttpClient getClient() {
        return client;
    }

    public RedissonClient getRedisson() {
        return redisson;
    }

    public ExecutorService getThreadPool() {
        return threadPool;
    }

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }

    public String getPlatform() {
        return DEFAULT_PLATFORM;
    }

    public String getShortLinkUrl() {
        return SHORT_LINK_URL;
    }

    public void setPlatform(String DEFAULT_PLATFORM) {
        this.DEFAULT_PLATFORM = DEFAULT_PLATFORM;
    }

    public void setShortLinkUrl(String SHORT_LINK_URL) {
        this.SHORT_LINK_URL = SHORT_LINK_URL;
    }
}
